package com.good.child.ui.utils;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

/**
 * Create on: 2024/8/4
 * by Mr.Zhu
 * Description: recycler分割线
 */
public class ShrinkingDividerItemDecoration extends RecyclerView.ItemDecoration {
    private Paint paint;
    private int dividerHeight;
    private boolean isShrunk;

    public ShrinkingDividerItemDecoration(int dividerHeight, int dividerColor) {
        this.dividerHeight = dividerHeight;
        isShrunk = false;
        paint = new Paint();
        paint.setColor(dividerColor);
    }

    public void shrinkDivider() {
        isShrunk = true;
    }

    public void expandDivider() {
        isShrunk = false;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (isShrunk) {
            return;
        }
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        for (int i = 0; i < parent.getChildCount(); i++) {
            View child = parent.getChildAt(i);
            int top = child.getBottom() + parent.getPaddingTop();
            int bottom = top + dividerHeight;
            c.drawRect(left, top, right, bottom, paint);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (!isShrunk) {
            outRect.set(0, 0, 0, dividerHeight);
        }
    }
}
